<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="会员ID" prop="memberId">
      <el-input v-model="dataForm.memberId" placeholder="会员ID"></el-input>
    </el-form-item>
    <el-form-item label="租户" prop="sellerId">
      <el-input v-model="dataForm.sellerId" placeholder="租户"></el-input>
    </el-form-item>
    <el-form-item label="消费金额" prop="consumeAmout">
      <el-input v-model="dataForm.consumeAmout" placeholder="消费金额"></el-input>
    </el-form-item>
    <el-form-item label="折扣" prop="discount">
      <el-input v-model="dataForm.discount" placeholder="折扣"></el-input>
    </el-form-item>
    <el-form-item label="时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="时间"></el-input>
    </el-form-item>
    <el-form-item label="创建人" prop="createUserId">
      <el-input v-model="dataForm.createUserId" placeholder="创建人"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :disabled="disabledSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          orderId: 0,
          memberId: '',
          sellerId: '',
          consumeAmout: '',
          discount: '',
          createTime: '',
          createUserId: '',
          remark: ''
        },
        dataRule: {
          memberId: [
            { required: true, message: '会员ID不能为空', trigger: 'blur' }
          ],
          sellerId: [
            { required: true, message: '租户不能为空', trigger: 'blur' }
          ],
          consumeAmout: [
            { required: true, message: '消费金额不能为空', trigger: 'blur' }
          ],
          discount: [
            { required: true, message: '折扣不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '时间不能为空', trigger: 'blur' }
          ],
          createUserId: [
            { required: true, message: '创建人不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.orderId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.orderId) {
            this.$http({
              url: this.$http.adornUrl(`/member/tstorememberconsumerecord/info/${this.dataForm.orderId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.memberId = data.tStoreMemberConsumeRecord.memberId
                this.dataForm.sellerId = data.tStoreMemberConsumeRecord.sellerId
                this.dataForm.consumeAmout = data.tStoreMemberConsumeRecord.consumeAmout
                this.dataForm.discount = data.tStoreMemberConsumeRecord.discount
                this.dataForm.createTime = data.tStoreMemberConsumeRecord.createTime
                this.dataForm.createUserId = data.tStoreMemberConsumeRecord.createUserId
                this.dataForm.remark = data.tStoreMemberConsumeRecord.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/member/tstorememberconsumerecord/${!this.dataForm.orderId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'orderId': this.dataForm.orderId || undefined,
                'memberId': this.dataForm.memberId,
                'sellerId': this.dataForm.sellerId,
                'consumeAmout': this.dataForm.consumeAmout,
                'discount': this.dataForm.discount,
                'createTime': this.dataForm.createTime,
                'createUserId': this.dataForm.createUserId,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
